114B - PFAST Inc - CodeForces Solution


bitmasks brute force graphs *1500

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
nodes = sorted([input() for _ in range(n)])
edges = {tuple(input().split()) for _ in range(m)}


n = len(nodes)
total_combinations = 2**n
team = []
for i in range(total_combinations):
    combination = []
    for j in range(n):
        if (i >> j) & 1:
            combination.append(nodes[j])
        
    conflict = False
    for a,b in edges:
        if a in combination and b in combination:
            conflict = True
            break
            
    if not conflict and len(team) < len(combination):
        team = combination
        
print(len(team))
for i in team:
    print(i)

C++ Code:

#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#define show(C) for (auto J: C) cout << J << " "; cout << endl;
#define ll long long
#define F(I, S, E) for(ll I = S; I < E; I++)
#define input(a,n) for(ll I = 0; I < n; I++){cin>>a[I];}
#define PYES cout << "YES\n"
#define PNO cout << "NO\n"
#define vi vector<ll>
#define pi pair<ll,ll>
#define f first
#define s second
#define pb push_back
using namespace std;
void solve()
{
    int n,m;cin>>n>>m;
    vector<string> person(n);
    vector<int> inc(n,0);
    map<string,int> mpp;
    for(int i=0;i<n;i++){cin>>person[i];mpp[person[i]]=i;}
    vector<vector<int> > adj(n);
    for(int i=0;i<m;i++)
    {
        string a,b;
        cin>>a>>b;
        adj[mpp[a]].push_back(mpp[b]);
        adj[mpp[b]].push_back(mpp[a]);
    }
    int len=0;
    for(int mask=0;mask<(1<<n);mask++)
    {
        // each of 2 to power n subset
        bool can=true;
        set<int> enemy;
        vector<int> temp(n,0);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            if(mask & (1<<i))
            {
                temp[i]=1;cnt++;
                if(enemy.find(i)!=enemy.end()){can=false;break;}
                else
                {
                    for(auto it:adj[i])enemy.insert(it);
                }
            }
        }
        if(can)
        {
            if(cnt>len)
            {
                len=cnt;
                for(int i=0;i<n;i++)inc[i]=temp[i];
            }
        }
    }
    cout<<len<<endl;
    set<string> res;
    for(int i=0;i<n;i++)
    {
        if(inc[i])res.insert(person[i]);
    }
    for(auto it:res)cout<<it<<endl;


}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
 
    return 0;
}


Comments

Submit
0 Comments
More Questions

1459B - Move and Turn
1006F - Xor-Paths
706C - Hard problem
304C - Lucky Permutation Triple
1301C - Ayoub's function
38E - Let's Go Rolling
171G - Mysterious numbers - 2
1183C - Computer Game
400C - Inna and Huge Candy Matrix
417A - Elimination
222A - Shooshuns and Sequence
1736A - Make A Equal to B
1736B - Playing with GCD
887C - Solution for Cube
1737C - Ela and Crickets
1741C - Minimize the Thickness
1741A - Compare T-Shirt Sizes
1741D - Masha and a Beautiful Tree
109B - Lucky Probability
1741B - Funny Permutation
1741E - Sending a Sequence Over the Network
344B - Simple Molecules
370A - Rook Bishop and King
546E - Soldier and Traveling
1741G - Kirill and Company
1200B - Block Adventure
1088B - Ehab and subtraction
1270B - Interesting Subarray
478C - Table Decorations
1304C - Air Conditioner